package com.gdk.ksoft.ui.reporters;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.dbhelper.FreporterDbHelper;
import com.gdk.ksoft.ui.reporters.adapters.OrderStatementAdapter;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;
import com.gdk.ksoft.assembly.utils.system.SQLiteUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick;

import java.util.HashMap;
import java.util.List;

/**
 * 订单日报查询页面
 */
public class OrderStatementActivity extends Activity {

    @ViewInject(R.id.porderStatement_txtTitle)private TextView txtTile;//标题文字
    @ViewInject(R.id.porderStatement_txtTotalMoney)private TextView txtTotalMoney;//总金额
    @ViewInject(R.id.porderStatement_txtTotalPiece)private TextView txtTotalPiece;//总张数
    @ViewInject(R.id.porderStatement_txtTotalCount)private TextView txtTotalCount;//总数量
    @ViewInject(R.id.porderStatement_lstStatement)private ListView lstStatement;//订单日报列表
    @ViewInject(R.id.porderStatement_txtNoUploadCount)private TextView txtNoUploadCount;//未上传数量
    private DbUtils dbUtils;//数据库操作对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_statement);
        ViewUtils.inject(this);
        dbUtils = SQLiteUtils.getInstance(this).getDbUtils();
        Intent intent = getIntent();
        if(intent!=null){
            String findDate = intent.getStringExtra("dateToFind");//获得查询的日期
            txtTile.setText(findDate+"订单日报");
            requestOrdersInfo(OrderStatementActivity.this,dbUtils,findDate);//开始查询订单信息
        }
    }

    /**
     * 查看未上传的
     * @param view
     */
    @OnClick(value={R.id.porderStatement_txtNoUploadCount})
    public void onViewClick(View view){

    }

    /**
     * 列表项点击事件
     * @param av
     * @param view
     * @param position
     * @param itemId
     */
    @OnItemClick(value = {R.id.porderStatement_lstStatement})
    public void onItemClick(AdapterView<?> av,View view,int position,long itemId){
        HashMap<String,Object> item = (HashMap<String,Object>)lstStatement.getItemAtPosition(position);
        Intent intent = new Intent(this,OrderStatementDetailActivity.class);
        intent.putExtra("orderNo",item.get("xdjh").toString());//写入单据号
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
    }

    /**
     * 请求单据信息
     */
    public void requestOrdersInfo(final Context context,final DbUtils dbUtils,final String date){
        new AsyncTask<Void,Void,Void>(){
            private ProgressDialog dialog;
            private String errorMessage = null;
            private List<HashMap<String,Object>> statementlst;
            private HashMap<String,Object> totalInfo;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog = ProgressDialog.show(OrderStatementActivity.this,"","加载中...");
            }
            @Override
            protected Void doInBackground(Void... params) {
                try{
                    statementlst = FreporterDbHelper.findAllOrderStatments(dbUtils,date,1);
                    totalInfo = FreporterDbHelper.findAllOrderTotalInfo(dbUtils,date,1);
                }catch (Exception e){
                    errorMessage = e.getMessage();
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                if(dialog!=null && dialog.isShowing()) dialog.dismiss();
                if (errorMessage != null) {
                    Toast.makeText(context, "查询出错了!原因：" + errorMessage, Toast.LENGTH_SHORT).show();
                    return;
                }
                lstStatement.setAdapter(new OrderStatementAdapter(context,statementlst));
                if(totalInfo!=null){//显示统计信息
                    txtNoUploadCount.setText(totalInfo.get("noupload").toString());
                    txtTotalCount.setText(totalInfo.get("totalXCount").toString());
                    txtTotalMoney.setText(FormatUtils.exactNumber(totalInfo.get("totalMoney"),"0.00")+"元");
                    txtTotalPiece.setText(totalInfo.get("totalCount").toString()+"张");
                }
            }
        }.execute();
    }

    @OnClick(value={R.id.porderStatement_txtBack})
    public void onBackClick(View view){
        this.finish();
        overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
            overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
